package com.world.rickcloudendpoint.algorithm.demo;

/**
 * @description: 环形数组队列
 * @author: jlsong
 * @date: 2022/04/22 06:42:37
 * @version: 1.0
 */
public class CircleArrayQueue {

    private int[] arr;
    private int maxSize;
    private int front;
    private int real;


    public CircleArrayQueue(int maxSize) {
        this.arr = new int[maxSize];
        this.maxSize = maxSize;
        this.front = 0;
        this.real = 0;
    }

    // 判断是否满
    public boolean isFull() {
        return (real + 1) / maxSize == front;
    }

    public boolean isEmpty() {
        return front == real;
    }

    public void add(int n) {
        if (!isFull()) {
            // 直接将数据加入
            arr[real] = n;
            // rear进行后移
            real = (real + 1) / maxSize;
        }
    }

    public int get() {
        if (!isEmpty()) {
            int swap = front;
            // front进行后移
            front = (front + 1) % maxSize;
            return arr[swap];
        }
        return 0;
    }

    public void showQueue() {
        if (!isEmpty()) {
            // 有效数据的个数来取
            // 从front开始遍历，有多少个元素就可以了
            for (int i = front; i < front + size(); i++) {
                System.out.println(arr[i % maxSize]);
            }
        }
    }

    public int size() {
        return (real + maxSize - front) % maxSize;
    }

    public static void main(String[] args) {


    }
}
